package com.zlp.mall.modules.pms.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlp.mall.common.api.Pager;
import com.zlp.mall.common.api.RespCode;
import com.zlp.mall.common.exception.CustomException;
import com.zlp.mall.common.util.BeanToUtils;
import com.zlp.mall.common.util.MethodUtil;
import com.zlp.mall.modules.pms.dto.req.BrandAddReq;
import com.zlp.mall.modules.pms.dto.resp.BrandResp;
import com.zlp.mall.modules.pms.mapper.BrandMapper;
import com.zlp.mall.modules.pms.model.Brand;
import com.zlp.mall.modules.pms.model.Product;
import com.zlp.mall.modules.pms.service.BrandService;
import com.zlp.mall.modules.pms.service.ProductService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 品牌表 服务实现类
 * </p>
 *
 * @author ZouLiPing
 * @since 2021-09-23
 */
@Service
@RequiredArgsConstructor
@Slf4j(topic = "BrandServiceImpl")
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    private final ProductService productService;


    @Override
    public Pager<BrandResp> list(String keyword, Integer pageNum, Integer pageSize) {

        log.info("list.req keyword={},pageNum={},pageSize={}",keyword,pageNum,pageSize);
        IPage<Brand> pages = new Page<>(pageNum, pageSize);
        // 构建查询条件
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Brand::getSort);
        if (StrUtil.isNotBlank(keyword)) {
            wrapper.like(Brand::getName,keyword);
        }
        IPage<Brand> page = this.page(pages, wrapper);
        List<Brand> records = page.getRecords();
        if (CollectionUtils.isNotEmpty(records)) {
            List<BrandResp> brandResps = BeanToUtils.entityToList(records, BrandResp.class);
            return new Pager(brandResps,page.getTotal(), pageSize, pageNum);
        }
        return new Pager<>();
    }

    @Override
    public boolean updateShowStatus(List<Long> ids, Integer showStatus) {

        log.info("updateShowStatus.req ids={},showStatus={}",ids,showStatus);
        LambdaUpdateWrapper<Brand> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.
                set(Brand::getShowStatus,showStatus).
                in(Brand::getId,ids);
        return this.update(updateWrapper);
    }

    @Override
    public BrandResp getInfoById(Long id) {

        log.info("getInfoById.req id={}",id);
        Brand brand = this.getById(id);
        if (Objects.isNull(brand)) {
            throw new CustomException(RespCode.BACK_PMS_3002, MethodUtil.getLineInfo());
        }
        return BeanToUtils.doToDto(brand,BrandResp.class);
    }

    @Override
    public boolean delete(Long id) {

        log.info("delete.req id={}",id);
        getInfoById(id);
        List<Product> brandList = productService.getProductByBrandId(id);
        if (CollectionUtil.isNotEmpty(brandList)) {
            throw new CustomException(RespCode.BACK_PMS_3001, MethodUtil.getLineInfo());
        }
        return this.removeById(id);
    }

    @Override
    public boolean createBrand(BrandAddReq brandAddReq) {

        log.info("createBrand.req brandAddReq={}", JSON.toJSONString(brandAddReq));
        Brand brand = new Brand();
        BeanUtils.copyProperties(brandAddReq, brand);
        //如果创建时首字母为空，取名称的第一个为首字母
        if (StringUtils.isEmpty(brand.getFirstLetter())) {
            brand.setFirstLetter(brand.getName().substring(0, 1));
        }
        return this.save(brand);
    }

    @Override
    public boolean updateBrand(Long id, BrandAddReq brandAddReq) {

        log.info("updateBrand.req id={},brandAddReq={}",id,JSON.toJSONString(brandAddReq));


        Brand brand = BeanToUtils.dtoToDo(brandAddReq, Brand.class);
        brand.setId(id);
        //如果创建时首字母为空，取名称的第一个为首字母
        if (StringUtils.isEmpty(brand.getFirstLetter())) {
            brand.setFirstLetter(brand.getName().substring(0, 1));
        }
        //更新品牌时要更新商品中的品牌名称
        productService.updateByBrandId(id,brandAddReq.getName());
        return this.updateById(brand);
    }
}
